package util.libraries;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;


/**
 * Allows for mapping of images to their filenames. Retains id-to-image
 * architecture as well
 * 
 * @author Julian
 */
public class ImageToFileNameLibrary extends ImageLibrary
{

    private static final String DEFAULT_DELIMITER = "\\.";
    protected ArrayList<String> myFileNames;
    protected String myFileType;


    public ImageToFileNameLibrary (String dir)
    {
        super(dir);
    }


    public ImageToFileNameLibrary (File dir)
    {
        super(dir);
    }


    public ImageToFileNameLibrary (String object, Dimension dimension)
    {
        super(new File(object), dimension);

    }


    @Override
    protected void addMappingElement (File add)
    {

        String[] split = add.getName().split(DEFAULT_DELIMITER);

        myFileNames.add(split[0]);
        if (myFileType == null) myFileType = split[1];
    }


    @Override
    protected void initialize ()
    {
        super.initialize();
        myFileNames = new ArrayList<String>();
    }


    public void setFileType (String fileType)
    {
        this.myFileType = fileType;
    }


    public String getFileType ()
    {
        return myFileType;
    }


    public Integer getIDByName (String string)
    {
        return myFileNames.lastIndexOf(string.split(DEFAULT_DELIMITER)[0]);
    }


    public BufferedImage getImageByName (String string)
    {
        return myImages.get(this.getIDByName(string));
    }
}
